package com.lxh.singleton;

/**
 * 单例类 只产生一个对象
 * Create by lxh on 2020/2/13
 */
public class Emperor {
//    private static Emperor emperor = null;
//    private static Object lock = new Object();

    private Emperor() {}

    // 会有线程安全问题 懒汉式
//    public static Emperor getInstance() {
//        if (emperor == null) {
//            emperor = new Emperor();
//        }
//        return emperor;
//    }

    // 线程安全 双重检验锁 多线程下能保证性能
//    public static Emperor getInstance() {
//        if (emperor == null) {
//            synchronized (lock) {
//                if (emperor == null) {
//                    emperor = new Emperor();
//                }
//            }
//        }
//        return emperor;
//    }


    // 最简单 饿汉式 线程安全 类加载就初始化，浪费内存
    private static Emperor emperor = new Emperor();
    public static Emperor getInstance() {
        return emperor;
    }
}
